package casel.info.onceuponatime;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet class Delete
 * 
 * @author Mikel Castillo Casel
 * vesion 1.0
 */
@WebServlet("/Delete")
public class Delete extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Delete() {
        super();
        // TODO Auto-generated constructor stub
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		deleteTale(request.getParameter("id"));
		
		this.getServletContext().
		getRequestDispatcher("/Home").
			forward(request,response);
	}
	/**
	 * removes a tale from database
	 * @param id
	 */
	private void deleteTale(String id) {
String urlcon = "jdbc:mysql://localhost:3306/onceupon";
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn = DriverManager.getConnection(urlcon, "root","");
			String sql= "delete from tale where id=?";
			PreparedStatement prep = conn.prepareStatement(sql);
			
			prep.setString(1, id);
			prep.addBatch();
			prep.executeBatch();
			
			conn.close();
					
		} catch (Exception e) {
			
			 e.printStackTrace();
			 
		}		
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
